package com.yanqu.road.server.cmd.activity.gourdchild;

import com.yanqu.road.entity.activity.gourdchild.GourdChildConfig;
import com.yanqu.road.entity.activity.gourdchild.GourdChildUserData;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.gourdchild.GourdChildModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.gourdchild.GourdChildMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_GOURD_CHILD_LOTTERY_DRAW, desc = "抽奖")
public class GourdChildLotteryDrawCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        GourdChildConfig config = GourdChildMgr.getConfig();
        if (config == null || ActivityMgr.activityInShowPeriod(config.getActivityInfo())) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }

        GourdChildModule module = player.getModule(GourdChildModule.class);
        GourdChildUserData userData = module.getUserData2();
        if (userData == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_UNLOCK;
            return;
        }

        long now = System.currentTimeMillis();
        if (now - userData.getLastDrawHideTime() < 3000) {
            ret = GameErrorCode.E_GOURD_CHILD_BOX_OPEN_FREQUENTLY;
            return;
        }

        // 是否足够道具
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(config.getMountainConsume()))) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_DRAW_ITEM_NO_ENOUGH;
            return;
        }

        userData.setLastDrawHideTime(now);

        ret = player.getModule(GourdChildModule.class).drawBox();

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            GourdChildProto.GourdChildLotteryDrawRespMsg.Builder respMsg = GourdChildProto.GourdChildLotteryDrawRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_GOURD_CHILD_LOTTERY_DRAW, respMsg);
        }
    }
}
